<template>
    <view class="wrap-login">
        <!-- 需要授权手机号 -->
        <button class="button-login" v-if="!is_bind_phone" open-type="getPhoneNumber" @getphonenumber="getphonenumber">
            <!-- <image class="button-img" src="/static/pay/wx_default.png"></image> -->
            <text>微信授权手机号</text>
        </button>
        <!-- 不需要授权手机号 -->
        <button class="button-login" v-else @click="getuserinfo" style="background:rgb(13,50,139)">
            <!-- <image class="button-img" src="/static/pay/wx_default.png"></image> -->
            <text>微信授权用户信息</text>
        </button>
    </view>
</template>

<script>
import * as loginApi from '@/common/apis/login-api.js';
import { insertMember } from '@/common/api';

export default {
    data() {
        return {
            is_bind_phone: false,
			testStr:0
        };
    },
    async onLoad() {
        // 验证是否已绑定手机，已绑定则不需要重新绑定
        await this.validatephone();
    },
    methods: {
        /** 验证openid是否绑定手机号 */
        async validatephone() {
            try {
                const result = await loginApi.validate({
                    openid: this.$store.state.user.openid,
                });
                this.is_bind_phone = !uni.$u.test.isEmpty(result);
            } catch (error) {
                this.is_bind_phone = false;
            }
        },
        /** 绑定手机号 */
        async getphonenumber(evt) {
			
            if (uni.$u.test.isEmpty(this.$store.state.user.deptId)) {
                uni.showToast({ title: '未选择门店', icon: 'error' });
                return;
            }
            await loginApi.bindphonenew({
                deptId: this.$store.state.user.deptId,
                openid: this.$store.state.user.openid,
				channelId: this.$store.state.user.channelId,
				userId: this.$store.state.user.userId,
				customerId: this.$store.state.user.customerId,
                encryptedData: evt.detail.encryptedData,
                iv: evt.detail.iv,
				sourceType: this.$store.state.user.sourceType,
            });
            uni.showToast({ title: '请继续授权用户信息', icon: 'success' })
            this.is_bind_phone = true;
        },
        /** 获取系统信息 */
        async getSystemInfo() {
            return new Promise((resolve, reject) => {
                uni.getSystemInfo({
                    success: (res) => {
                        resolve(res);
                    },
                    fail: (err) => {
                        uni.showToast({ title: '系统获取失败', icon: 'error' });
                        reject(err);
                    },
                })
            })
        },
        /** 绑定用户信息 */
        getuserinfo() {
            uni.getUserProfile({
                desc: '绑定用户信息',
                success: (detail) => {
                    loginApi.savewxinfo({
                        openid: this.$store.state.user.openid,
                        nickName: detail.userInfo.nickName.replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g, "口"),
                        portraitImg: detail.userInfo.avatarUrl,
                        gender: detail.userInfo.gender,
                        address: `${detail.userInfo.country}-${detail.userInfo.province}-${detail.userInfo.city}`,
                        sourceType: this.$store.state.user.sourceType,
                    }).then(() => {
                        return loginApi.customerinfo({
                            openid: this.$store.state.user.openid,
                        });
                    }).then(res => {
                        this.$store.commit('user/saveUserInfo', res);
                        uni.setStorageSync('userInfo', res);
						console.log('发射登录成功');
						uni.$emit('login', true);
                        return this.insertMember(res);
                    }).then(res => {
                        console.log(res);
                        if (res == '操作成功') {
                            uni.navigateBack();
                        }
                    })
                },
                fail: (error) => {
                    console.log(error);
                },
            })
        },
        /** 新增业务客户 */
        async insertMember(userInfo) {
            const { model } = await this.getSystemInfo();
			
			return '操作成功';
			
            // return insertMember({
            //     memberNo: userInfo.customerId, // 客户编号
            //     memberName: userInfo.nickName.replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g, "口"), // 客户名称
            //     vxUrl: userInfo.portraitImg, // 微信图像
            //     vxName: userInfo.nickName.replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g, "口"), // 微信昵称
            //     memberPhone: userInfo.phone, // 联系方式
            //     phoneModel: model, // 手机型号
            //     tenantId: this.$store.state.user.tenantCode, // 租户id
            //     inviterId: this.$store.state.user.inviterId, // 分享上级客户编号
            //     qrCode: `pages/mine/mine?scene=${userInfo.customerId}`, // 当前客户分享二维码地址
            //     openId: this.$store.state.user.openid, // openId
            // });
        },
    },
}
</script>

<style lang="scss" scoped>
// page{
	// background-color: rgba(0, 0, 0, 0)
	// background: transparent;
// }
	
.wrap-login{
	width: 100vw;
	height: 100vh;
	display: flex;
	background: transparent;
	align-items: center;
	justify-content: space-between;
}
.button-login {
	width: 80%;
	height: 88upx;
	display: flex;
	background-color: #5fbe67;
	color:#fff;
	font-size: 30upx;
	border: none;
	border-radius: 44upx;
	justify-content: center;
	align-items: center;
	font-weight: 400;
	margin: 255upx auth 74upx;
	text{
		color: #fff;
	}
	.button-img {
		width: 40upx;
		height: 40upx;
		margin-right: 6upx;
	}
}
</style>
